<template>
  <a-drawer
    :visible="formVisible"
    :width="500"
    unmount-on-close
    :title="isEdit ? '编辑' : '新增'"
    @ok="submit"
    @cancel="close"
  >
    <div>
      <a-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-align="right"
        :label-col-props="{ span: 8 }"
        :wrapper-col-props="{ span: 16 }"
      >
        <a-form-item field="name" label="机构名称">
          <a-input v-model="form.name" placeholder="请填写机构名称" />
        </a-form-item>
        <a-form-item field="code" label="机构编码">
          <a-input v-model="form.code" placeholder="请填写机构编码" />
        </a-form-item>
        <a-form-item field="type" label="机构类型">
          <a-select
            v-model="form.type"
            :options="ORG_TYPES"
            placeholder="请选择机构类型"
          ></a-select>
        </a-form-item>
        <a-form-item field="parentId" label="上级机构">
          <a-tree-select
            v-model="form.parentId"
            :field-names="{
              key: 'id',
              title: 'name',
              children: 'children',
            }"
            :data="renderData"
            placeholder="请选择上级机构"
          />
        </a-form-item>
        <a-form-item label="排序" field="sort">
          <a-input-number v-model="form.sort" />
        </a-form-item>
        <a-form-item field="phone" label="电话">
          <a-input v-model="form.phone" placeholder="请填写电话" />
        </a-form-item>
        <a-form-item field="email" label="邮箱">
          <a-input v-model="form.email" placeholder="请填写邮箱" />
        </a-form-item>
        <a-form-item field="address" label="地址">
          <a-input v-model="form.address" placeholder="请填写地址" />
        </a-form-item>
        <a-form-item field="remarks" label="备注">
          <a-input v-model="form.remarks" />
        </a-form-item>
      </a-form>
    </div>
  </a-drawer>
</template>

<script lang="ts" setup>
  import { FieldRule, Message } from '@arco-design/web-vue';
  import { ref, reactive, watch, computed } from 'vue';
  import { addOrg, getOrgTree, ORG_TYPES, updateOrg } from '@/api/upms/org';
  import { useRequest } from 'vue-request';
  import { useVisible } from '@/hooks/visible';

  const { data } = useRequest(getOrgTree);

  const renderData = computed(() => data.value || []);

  const formRef = ref();

  const initData = reactive({
    id: '',
    username: '',
    password: '',
    orgId: '',
    roleIds: [],
    phone: '',
    // 其他
    userType: 0,
    lockFlag: '0',
  });

  const rules: Record<string, FieldRule[]> = {
    name: [
      {
        type: 'string',
        required: true,
        message: '机构名不能为空',
      },
    ],
    code: [
      {
        type: 'string',
        required: true,
        message: '机构编码不能为空',
      },
    ],
    parentId: [
      {
        type: 'string',
        required: true,
        message: '上级组织机构不能为空',
      },
    ],
    type: [
      {
        type: 'string',
        required: true,
        message: '机构类型不能为空',
      },
    ],
  };

  const props = defineProps({
    visible: {
      type: Boolean,
      default: false,
    },
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  });

  const emit = defineEmits<{
    (e: 'update:visible', v: boolean): void;
    (e: 'refresh'): void;
  }>();
  const { formVisible, close } = useVisible(props, emit);

  const form = ref(props.formData);

  // 判断是编辑还是新增
  const isEdit = computed(() => !!form.value.id);

  const submit = async () => {
    formVisible.value = true;
    const validate = await formRef.value.validate();
    if (!validate) {
      if (isEdit.value) {
        await updateOrg(form.value).then(() => {
          Message.success('修改成功');
        });
      } else {
        await addOrg(form.value).then(() => {
          Message.success('添加成功');
        });
      }
      form.value = { ...initData };
      formVisible.value = false;
      emit('refresh');
    }
  };

  watch(
    () => props.formData,
    (val) => {
      form.value = val;
    }
  );
</script>
